<template>
  <div class="search">
      <!-- <div class="se-title">
          <img src="../../assets/title.png" alt="">
      </div> -->
      <div class="se-nav">
          <div class="top">
                <p  class="topp" :class="!boos?'active':''" @click="shang">商品</p>
                <p  class="topp" :class="boos?'active':''" @click="jing">经销商</p>
          </div>
          <div class="bottom">
              <img src="../../assets/Fill 1.png" alt="" @click="tiao">
              <input type="text" placeholder="搜索商品" v-model="search">
              <p @click="look;tiao2()">搜索</p>
              <img src="../../assets/qingkong@2x.png" alt="" class="shan" v-if="this.search!=''" @click="deletwo">
          </div>
      </div>
      <div class="se-history">
          <div class="his-top">
              <p>搜索历史</p> 
              <img src="../../assets/shan.png" alt="" @click="dele">
          </div>
          <div class="his-list">
                <ul class="lists" >
                    <li class="lili" v-for="(i,index) in Searchlist" :key="index" >{{i.txt}}</li>
                </ul>
          </div>
      </div>

  </div>
</template>

<script>
import Vue from 'vue'
import { Dialog } from 'vant';

Vue.use(Dialog);
export default {
   name:'search',
    data(){
        return{
            boos:true,
            ifs:true,
            show:false,
            search:'',
            Searchlist:[]
        }
    },
    methods:{
        shang(){
            this.boos=!this.boos
        },
        jing(){
            this.boos=!this.boos
        },
        tiao(){
            this.$router.back({
                path:'/home'
            })
        },
        tiao2(){
            this.$router.push({
                path:'/search2'
            })
        },
        dele(){
            Dialog.confirm({
                title: '确定删除全部搜索历史记录',
                }).then(() => {
                    this.Searchlist=[]
                    localStorage.removeItem('Searchlist')
                }).catch(()=>{
                    
                })
        },
        deletwo(){
            this.search=''
        },
        look(){  
            this.setSearchlist()
        },
        getSearchHistory(){
            let Searchlist;
            if(localStorage.getItem('Searchlist')){
                Searchlist=JSON.parse(localStorage.getItem('Searchlist'))
            }else{
                Searchlist=[]
            }
            this.Searchlist=Searchlist
            console.log(Searchlist)
        },
        setSearchlist(){
            let Searchlist=this.Searchlist;
            // console.log(loginInfolist)
            for(let i=0;i<this.Searchlist.length;i++){
                if(Searchlist[i].txt==this.search){
                    Searchlist.splice(i,1);
                    break;
                }
            }
            Searchlist.unshift({
                txt:this.search
            })
            if(Searchlist.length>9){
                Searchlist.splice(9,Searchlist.length-9)
                
            }
            this.Searchlist=Searchlist
            localStorage.setItem('Searchlist',JSON.stringify(Searchlist))
        }
    },
    created(){
     this.getSearchHistory()
    }
}

</script>
<style scoped lang="scss"> 
    li{list-style: none;}
    .search{
        background: #F2F1F6;
        height: 6.67rem;
        .se-title{
            height: 0.3rem;
            width: 100%;
            display: flex;
            background: #fff;
            align-items: center;
            img{
                height: 0.2rem;
                width: 100%;
            }
        }
        .se-nav{
            height: 0.8rem;
            display: flex;
            background: #fff;
            flex-direction: column;
            line-height: 0.8rem;
            .top{
                display: flex;
                margin: 0.1rem 0.1rem 0.1rem 0.4rem;
                .topp{
                    font-size: 0.14rem;
                    color: #333333;
                    line-height: 0.14rem;
                    margin-right: 0.2rem;
                }
                .active{
                    color: #DD3333;
                }
            }
            .bottom{
                height: 0.33rem;
                display: flex;
                align-items: center;
                justify-content: space-around;
                position: relative;
                img{
                    height: 0.15rem;
                    width: 0.08rem;
                }
                .shan{
                    position: absolute;
                    right: 0.7rem;
                    height: 0.15rem;
                    width: 0.14rem;
                }
                input{
                    height: 0.33rem;
                    width: 2.77rem;
                    background: #FFFFFF;
                    border: 0.01rem solid #ccc;
                    border-radius: 0.05rem;
                    padding-left:0.4rem; 
                    box-sizing: border-box;
                    background-image: url('../../assets/sousuo.png');
                    background-repeat: no-repeat;
                    background-size: 0.14rem;
                    background-position: 0.1rem;
                    outline: none;
                    font-size: 0.14rem;
                }
                p{
                    font-size: 0.16rem;
                    color: #DD3333;
                    line-height: 0.16rem;
                }
            }
        }
        .se-history{
            height: 1.16rem;
            background: #fff;
            .his-top{
                height: 0.47rem;
                display: flex;
                align-items: center;
                justify-content: space-between;
                padding: 0.1rem;
                box-sizing: border-box;
                p{
                    font-size: 0.14rem;
                    color: #333333;
                    letter-spacing: -0.17px;
                    line-height: 0.14rem;
                }
                img{
                    height: 0.12rem;
                    width: 0.12rem;
                }
            }
            .his-list{
                height: 0.55rem;
                .lists{
                    display: flex;
                    flex-wrap: wrap;
                    align-items: center;
                    height: 0.55rem;
                    .lili{
                        margin-left: 0.15rem;
                        height: 0.18rem;
                        padding: 0 0.03rem 0 0.03rem;
                        margin-right: 0.05rem;
                        background: #F2F1F6;
                        border: 0.01rem solid #F2F1F6;
                        border-radius: 0.12rem;
                        font-size: 0.12rem;
                        color: #666666;
                        line-height: 0.18rem;
                        text-align: center;
                    }
                }
            }
        }
    }
</style>